home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Viewers / aa_m68k_Intel_Only / ToyViewer1.2 / Source / mag2pxo.tproj / sjis2euc.c < prev   
Encoding:
C/C++ Source or Header  |  1995-09-21  |  1.4 KB  |  68 lines

  1. /***********************************************************
  2.     sjis2euc.c
  3.     1995-04-14  by T.Ogihara
  4. ***********************************************************/
  5. #define  iskanji(c)    (((c)>=0x81 && (c)<=0x9f) || ((c)>=0xe0 && (c)<=0xfc))
  6.     /* ¥•¥à¥¨JIS 1¥—¥⁄¥¨³è */
  7. #define  iskanji2(c)    ((c)>=0x40 && (c)<=0xfc && (c)!=0x7f)
  8.     /* ¥•¥à¥¨JIS 2¥—¥⁄¥¨³è */
  9. #define  is8kana(c)    ((c)>=0xa0 && (c)<0xe0)
  10.     /* ¨¬‡±¥«¥˚ */
  11. #define  XK        0x8e    /* EUC¨¬‡±¥«¥˚ */
  12.  
  13. static void conv(int *ph, int *pl)  /* ¥•¥à¥¨JIS⁄ù EUC⁄¸ */
  14. {
  15.     if (*ph <= 0x9F) {
  16.         if (*pl < 0x9F)  *ph = (*ph << 1) - 0xE1;
  17.         else             *ph = (*ph << 1) - 0xE0;
  18.     } else {
  19.         if (*pl < 0x9F)  *ph = (*ph << 1) - 0x161;
  20.         else             *ph = (*ph << 1) - 0x160;
  21.     }
  22.     if      (*pl < 0x7F) *pl -= 0x1F;
  23.     else if (*pl < 0x9F) *pl -= 0x20;
  24.     else                 *pl -= 0x7E;
  25.     *ph |= 0x80;
  26.     *pl |= 0x80;
  27. }
  28.  
  29. void sjis2euc(unsigned char *dst, unsigned char *src)
  30. {
  31.     int c, d;
  32.  
  33.     for (c = *src; c; ) {
  34.         if (c & 0x80) {
  35.             if (is8kana(c))
  36.                 *dst++ = XK, *dst++ = c;
  37.             else if (iskanji(c)) {
  38.                 d = *++src;
  39.                 if (iskanji2(d)) {
  40.                     conv(&c, &d);
  41.                     *dst++ = c, *dst++ = d;
  42.                 }else {
  43.                     *dst++ = c & 0x7f;
  44.                     c = d;
  45.                     continue; /* ERROR */
  46.                 }
  47.             }else
  48.                 *dst++ = c & 0x7f;
  49.         }else
  50.             *dst++ = c;
  51.         c = *++src;
  52.     }
  53.     *dst = 0;
  54. }
  55.  
  56. #ifdef ALONE
  57. #include <stdio.h>
  58. main()
  59. {
  60.     unsigned char buf[256], dst[256];
  61.  
  62.     while(gets(buf)) {
  63.         sjis2euc(dst, buf);
  64.         puts(dst);
  65.     }
  66. }
  67. #endif
  68.